package com.xh.bussiness.teachbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.teachbook.bean.request.SaveTeachingBookFeedbackRequest;
import com.xh.bussiness.teachbook.dao.TeachingBookFeedbackMapper;
import com.xh.bussiness.teachbook.model.TeachingBook;
import com.xh.bussiness.teachbook.model.TeachingBookFeedback;
import com.xh.bussiness.teachbook.service.ITeachingBookFeedbackService;
import com.xh.bussiness.teachbook.service.ITeachingBookService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * </p>
 *
 * @author Pitt zhang
 * @since 2019/9/24
 */
@Service
public class TeachingBookFeedbackServiceImpl extends BaseServiceImpl<TeachingBookFeedbackMapper, TeachingBookFeedback> implements ITeachingBookFeedbackService {
    @Autowired
    private ITeachingBookService teachingBookService;

    @Override
    public void add(SaveTeachingBookFeedbackRequest request) {
        TeachingBookFeedback teachingBookFeedback = this.queryByTeachingBookId(request.getTeachingBookId());
        if (teachingBookFeedback != null) {
            BeanUtils.copyProperties(request, teachingBookFeedback);
            this.update(teachingBookFeedback);
        } else {
            teachingBookFeedback = new TeachingBookFeedback();
            BeanUtils.copyProperties(request, teachingBookFeedback);
            this.insert(teachingBookFeedback);
        }
        String teachingBookId = request.getTeachingBookId();
        TeachingBook teachingBook = teachingBookService.getById(teachingBookId);
        if (teachingBook != null) {
            if (request.getSaveTarget().equals(1)) {
                teachingBook.setClassExamine(1);
            } else {
                teachingBook.setClassFeedback(1);
            }
            teachingBookService.update(teachingBook);
        }
    }

    public TeachingBookFeedback queryByTeachingBookId(String teachingBookId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("teaching_book_id", teachingBookId);
        TeachingBookFeedback teachingBookFeedback = this.dao.selectOne(qw);
        return teachingBookFeedback;
    }

}
